/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAO.DAO_Account;
import DAO.DAO_Connect;
import Entity.Account;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author THANHDAT
 */
@ManagedBean
@RequestScoped
public class AccountBean {

    int id;
    String username;
    String password;
    String fullname;
    String phone;
    String address;
    String email;
    String role = "0";

    /** Creates a new instance of AccountBean */
    public AccountBean() {
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

//    public SelectItem[] getListRoles(){
//        return new SelectItem[]{
//            new SelectItem("0", "User"),
//            new SelectItem("1", "Admin")
//        };
//    }
    public void addUser() {
        try {
            String query = "select * from tblAccounts";
            PreparedStatement pst = DAO_Connect.getConnection().prepareCall(query);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                String uname = rs.getString("accUserName");
                if (username.equals(uname)) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Account is exits.", "Account is exits."));
                    return;
                }
            }
            DAO_Account ac = new DAO_Account();
            ac.addAccount(username, password, fullname, phone, address, email, role);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Register Successfull.", "Register Successfull."));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
